package com.example.app.database;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.app.data.MMissionBean;

import java.util.ArrayList;
import java.util.List;

public class DBMissionData extends SQLiteOpenHelper {

    private SQLiteDatabase db;

    public DBMissionData(Context context){
        super(context,"mission.db",null,1);
        db=getReadableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS mission(" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "missionNumber INTEGER," +
                "missionName TEXT,"+
                "missionPlace TEXT,"+
                "securityLevel TEXT,"+
                "checkProcess TEXT,"+
                "ifPass INTEGER)");
    }

    public void setPass(int position)
    {
        db.execSQL("UPDATE mission SET ifPass = ? WHERE missionNumber = ?",new Object[]{1,position});
    }

    public void deleteAll(){
        db.execSQL("delete from mission");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
            db.execSQL("DROP TABLE IF EXISTS mission");
            onCreate(db);
    }

    public void add(int num,String name,String place,String level,String process,int pass){
        Cursor cursor=db.query("mission",null,null,null,null,null,"missionName DESC");
        cursor.moveToFirst();
        boolean i=false;
        while(cursor.moveToNext()){
            @SuppressLint("Range") int t = cursor.getInt(cursor.getColumnIndex("missionNumber"));
            if(num==t){
                i=true;
                break;
            }
        }
        if(!i)
            db.execSQL("INSERT INTO mission (missionNumber,missionName,missionPlace,securityLevel,checkProcess,ifPass) VALUES(?,?,?,?,?,?)",new Object[]{num,name,place,level,process,pass});
    }

    public void add(MMissionBean bean){
        int num=bean.getMissionNumber();
        Cursor cursor=db.query("mission",null,null,null,null,null,"missionName DESC");
        cursor.moveToFirst();
        boolean i=false;
        while(cursor.moveToNext()){
            @SuppressLint("Range") int t = cursor.getInt(cursor.getColumnIndex("missionNumber"));
            if(num==t){
                i=true;
                break;
            }
        }
        if(!i)
        {
            String name=bean.getMissionName();
            String place=bean.getMissionPlace();
            String level=bean.getSecureLevel();
            String process=bean.getCheckSchedule();
            int pass=bean.getIfPass();
            db.execSQL("INSERT INTO mission (missionNumber,missionName,missionPlace,securityLevel,checkProcess,ifPass) VALUES(?,?,?,?,?,?)",new Object[]{num,name,place,level,process,pass});
        }
    }

    public void delete(int num){
        db.execSQL("DELETE FROM mission WHERE missionNumber ="+num);
    }

    public void upData(String password){
        db.execSQL("UPDATE mission SET password = ?",new Object[]{password});
    }

    public MMissionBean getData(int position){
        Cursor cursor=db.query("mission",null,null,null,null,null,"missionName DESC");
        MMissionBean bean=new MMissionBean();
        cursor.moveToFirst();
        do
        {
            @SuppressLint("Range") int num = cursor.getInt(cursor.getColumnIndex("missionNumber"));
            if(num==position)
            {
                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("missionName"));
                @SuppressLint("Range") String place = cursor.getString(cursor.getColumnIndex("missionPlace"));
                @SuppressLint("Range") String level = cursor.getString(cursor.getColumnIndex("securityLevel"));
                @SuppressLint("Range") String process = cursor.getString(cursor.getColumnIndex("checkProcess"));
                @SuppressLint("Range") int pass = cursor.getInt(cursor.getColumnIndex("ifPass"));
                bean.setMissionNumber(num);
                bean.setMissionName(name);
                bean.setMissionPlace(place);
                bean.setSecureLevel(level);
                bean.setCheckSchedule(process);
                bean.setIfPass(pass);
                break;
            }
        }while (cursor.moveToNext());
        return bean;
    }

    public List<MMissionBean> getAllData(){

        List<MMissionBean> list = new ArrayList<MMissionBean>();
        Cursor cursor = db.query("mission",null,null,null,null,null,"missionName DESC");
        while(cursor.moveToNext()){
            @SuppressLint("Range") int num = cursor.getInt(cursor.getColumnIndex("missionNumber"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("missionName"));
            @SuppressLint("Range") String place = cursor.getString(cursor.getColumnIndex("missionPlace"));
            @SuppressLint("Range") String level = cursor.getString(cursor.getColumnIndex("securityLevel"));
            @SuppressLint("Range") String process = cursor.getString(cursor.getColumnIndex("checkProcess"));
            @SuppressLint("Range") int pass = cursor.getInt(cursor.getColumnIndex("ifPass"));
            MMissionBean bean=new MMissionBean();
            bean.setMissionNumber(num);
            bean.setMissionName(name);
            bean.setMissionPlace(place);
            bean.setSecureLevel(level);
            bean.setCheckSchedule(process);
            bean.setIfPass(pass);
            list.add(bean);
        }
        return list;
    }
}
